﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eBook.WinApp
{
  public static class ControlExtensions
  {
    public static void DataBindTo(this TextBox control, object instance, string propertyName)
    {
      DataBindTo(control, instance, propertyName, null);
    }

    public static void DataBindTo(this TextBox control, object instance, string propertyName, EventHandler valueChangedHandler)
    {
      ControlExtensions.AddBinding(control, new Binding("Text", instance, propertyName));
      if ( valueChangedHandler != null )
        control.TextChanged += new EventHandler(valueChangedHandler);
    }

    public static void DataBindTo(this MaskedTextBox control, object instance, string propertyName)
    {
      DataBindTo(control, instance, propertyName, null);
    }

    public static void DataBindTo(this MaskedTextBox control, object instance, string propertyName, EventHandler valueChangedHandler)
    {
      ControlExtensions.AddBinding(control, new Binding("Text", instance, propertyName));
      if ( valueChangedHandler != null )
        control.TextChanged += new EventHandler(valueChangedHandler);
    }

    public static void DataBindTo(this ComboBox control, object instance, string propertyName)
    {
      DataBindTo(control, instance, propertyName, null);
    }

    public static void DataBindTo(this ComboBox control, object instance, string propertyName, EventHandler valueChangedHandler)
    {
      ControlExtensions.AddBinding(control, new Binding("SelectedValue", instance, propertyName));
      if ( valueChangedHandler != null )
        control.SelectedValueChanged += new EventHandler(valueChangedHandler);
    }

    public static void DataBindTo(this DateTimePicker control, object instance, string propertyName)
    {
      DataBindTo(control, instance, propertyName, null);
    }

    public static void DataBindTo(this DateTimePicker control, object instance, string propertyName, EventHandler valueChangedHandler)
    {
      ControlExtensions.AddBinding(control, new Binding("Value", instance, propertyName));
      if ( valueChangedHandler != null )
        control.ValueChanged += new EventHandler(valueChangedHandler);
    }

    public static void DataBindTo(this CheckBox control, object instance, string propertyName)
    {
      DataBindTo(control, instance, propertyName, null);
    }

    public static void DataBindTo(this CheckBox control, object instance, string propertyName, EventHandler valueChangedHandler)
    {
      ControlExtensions.AddBinding(control, new Binding("Checked", instance, propertyName));
      if ( valueChangedHandler != null )
        control.CheckedChanged += new EventHandler(valueChangedHandler);
    }

    private static void AddBinding(Control control, Binding binding)
    {
      control.DataBindings.Clear();
      control.DataBindings.Add(binding);    
    }

  }


}
